from app.models import Carousel
from app import db
from exts import JsonConverter, BeanUtils


def carousel_add(carousel):
    '''
    添加轮播图
    :param carousel:
    :return:
    '''
    o = JsonConverter.dict_to_object(carousel, Carousel)
    db.session.add(o)
    db.session.commit()


def carousel_update(carousel):
    '''
    更新轮播图
    :param carousel:
    :return:
    '''
    '''
    :param news:
    :return:
    '''
    o = Carousel.query.get(carousel['id'])
    # 对象复制
    BeanUtils.copy(carousel, o)
    db.session.commit()


def carousel_delete(id):
    '''
    删除轮播图
    :param id:
    :return:
    '''
    o = Carousel.query.get(id)
    db.session.delete(o)
    db.session.commit()


def carousel_detail(id):
    '''
    查询轮播详情
    :param id:
    :return:
    '''
    return Carousel.query.get(id)


def carousel_get_by_id(id):
    '''
    查询轮播详情
    :param id:
    :return:
    '''
    return Carousel.query.get(id)


def carousel_page(pageNum, pageSize):
    '''
    分页查询轮播
    :param params:
    :return:
    '''
    return Carousel.query.paginate(page=pageNum, per_page=pageSize)


def carousel_all():
    '''
    查询所有轮播
    :return:
    '''
    return Carousel.query.all()
